package mfuny.ggongal.admin.terms;

import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;

import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class TermsService {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	
	public TermsService() throws Exception {
		sqlMapper = Server.getSqlMapper( );
		mav = new ModelAndView();
	}
	
	//약관 리스트 폼.
	public ModelAndView termsList() throws Exception {
		mav.clear( );
		mav.setViewName( "/terms/termsList.ga" );
		return mav;
	}
	
	//약관 리스트 결과.
	@SuppressWarnings( "unchecked" )
	public ModelAndView termsList_result( int terms_type ) throws Exception {
		List<HashMap<String, Object>> termsList = sqlMapper.queryForList( "terms_selectSubject", terms_type );
		mav.clear( );
		mav.setViewName( "/terms/termsList_result" );
		mav.addObject( "termsList", termsList );
		return mav;
	}
	
	//약관 등록하기 폼.
	public ModelAndView regTermsForm() throws Exception {
		mav.clear( );
		mav.setViewName( "/terms/regTermsForm.ga" );
		return mav;
	}
	
	//약관 등록처리.
	public ModelAndView regTerms( TermsDao termsClass ) throws Exception {
		termsClass.setContent( termsClass.getContent().replaceAll("\r\n", "\\\\n") );
		sqlMapper.update( "terms_insert", termsClass );
		return termsList();
	}
	
	//약관 내용보기.
	public ModelAndView readTerms( int terms_no ) throws Exception {
		TermsDao termsClass = (TermsDao) sqlMapper.queryForObject( "terms_selectByNo", terms_no );
		termsClass.setContent(termsClass.getContent().replaceAll("\\\\n", "\n"));
		mav.clear( );
		mav.setViewName( "/terms/readTerms.ga" );
		mav.addObject( "termsClass", termsClass );
		return mav;
	}
	
	//약관 삭제처리.
	public ModelAndView deleteTerms( int terms_no ) throws Exception {
		int result = sqlMapper.delete( "terms_delete", terms_no );
		mav.clear( );
		mav.setViewName( "/blank" );
		mav.addObject( "value", result );
		return mav;
	}
	
	//약관 수정하기 폼. 
	public ModelAndView modifyTermsForm( int terms_no ) throws Exception {
		TermsDao termsClass = (TermsDao) sqlMapper.queryForObject( "terms_selectByNo", terms_no );
		termsClass.setContent( termsClass.getContent().replaceAll("\\\\n", "\n") );
		mav.clear();
		mav.setViewName("/terms/modifyTermsForm.ga");
		mav.addObject("termsClass", termsClass);
		return mav;
	}
	
	//약관 수정처리.
	public ModelAndView modifyTerms(TermsDao termsClass ) throws Exception {
		termsClass.setContent( termsClass.getContent().replaceAll("\r\n", "\\\\n"));
		sqlMapper.update( "terms_update", termsClass );
		return termsList();
	}
}